<?php
/**
 * 首页
 * david.yan (david.yan@qq.com) by 2013
 * 北京泰和盈动科技有公司 版权所有
 */
class system extends actionAbstract {

    public $title = '';
    public $css = array();
    public $modules = array();
    public $menu = array();
    public $pos = 0;
	public $type=0;
	public $leftpos=0;
    
	/**
	* 递归要添加的菜单PIN
	 *
	 * @var array
	 */
	protected $add_auth = array ();
	
    /**
     * 构造
     */
    function __construct() {
        parent::__construct();
          $this->info = acl::checkLogin('admininfo',$this->url('common/login'));
        //     if(!isset($_SESSION['admininfo'])){ header('location:'.$this->url('common/login'));}
		$this->menu = $this->loadConfig(); 
		$this->type = isset($_GET['type'])?(int)$_GET['type']:0;
    }

    /**
     * 平台设置
     */
    public function website(){
		$this->leftpos=0;
        $this->loadModel('sys','setting');
        $re=$this->sys->settingModel->fetchRow("select * from sys_setting");
        
        include $this->loadWidget('masterTheme');
    }
    /**
     * 平台设置修改
     */
    public function check(){
        $this->loadHelper('extend');
        $this->loadModel('sys','setting');
        if($_POST){
            $data=isset($_POST['data'])?$_POST['data']:'';
            $result=$this->sys->settingModel->fetchRow("select * from sys_setting");
            if($result){
                $re=$this->sys->settingModel->update($data,$result['id']);
            }else{
                $re=$this->sys->settingModel->insert($data);
            }
            if($re){
                ajaxReturn ('back','更新成功！', 1 );exit;
            }else{
                ajaxReturn ('','更新失败！', 0 );exit;
            }
        }
    }
    
    /**
     * 帐号管理
     */
    public function admin(){
        $this->leftpos=1;
        $this->loadModel('sys','account');
        $re=$this->sys->accountModel->fetchAll("select * from sys_account");
        
        include $this->loadWidget('masterTheme');
    }
    /**
     * 进入修改账号
     */
    public function edit(){
        $this->leftpos=1;
        $this->loadModel('sys','account');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?(int)($_GET['id']):0;
            $sql="select * from sys_account where id=$id";
            $re=$this->sys->accountModel->fetchRow($sql);
        }
        if($_POST){
            $data=isset($_POST['data'])?$_POST['data']:'';
            $id=isset($_POST['id'])?$_POST['id']:0;
            $password=isset($_POST['password'])?$_POST['password']:'';
            $pwd=isset($_POST['pwd'])?$_POST['pwd']:'';
            $setname=$this->sys->accountModel->fetchRow("select * from sys_account where username='".$data['username']."' and id!=$id");
            if($setname){
                ajaxReturn ('','该账号已存在！', 0 );exit;
            }
            //修改
            if($id){
                if(!empty($password)||!empty($pwd)){
                    if($password==$pwd){
                        $data['password']=md5($password);
                    }else{
                        ajaxReturn ('','两次密码不一致！', 0 );exit;
                    }
                }
                $re=$this->sys->accountModel->update($data,$id);
            //添加
            }else{
                if(trim($data['username'])==''){
                    ajaxReturn ('','请填写账号！', 0 );exit;
                }
                if(trim($password)==''){
                    ajaxReturn ('','请填写密码！', 0 );exit;
                }
                if($password!=$pwd){
                    ajaxReturn ('','两次密码不一致！', 0 );exit;
                }
                $data['password']=md5($password);
                $data['createtime']=time();
                $re=$this->sys->accountModel->insert($data);
            }
            if($re){
                ajaxReturn ('back','操作成功！', 1 );exit;
            }else{
                ajaxReturn ('','操作失败！', 0 );exit;
            }
        }
        
        include $this->loadWidget('masterTheme');
    }
    /**
     * 删除操作
     */
    public function del(){
        $this->leftpos=1;
        $this->loadHelper('extend');
        $this->loadModel('sys','account');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $re=$this->sys->accountModel->delete($id);
            if($re){
                ajaxReturn ('back','删除成功', 1 );exit;
            }else{
                ajaxReturn ('','删除失败', 0 );exit;
            }
        }
    }
    /**
    *  修改账号状态
    */
    public function status(){
        $this->leftpos=1;
        $this->loadHelper('extend');
        $this->loadModel('sys','account');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $result=$this->sys->accountModel->fetchRow("select * from sys_account where id=$id");
            if($result&&$result['status']==1){
                $data['status']=2;
            }elseif($result&&$result['status']==2){
                $data['status']=1;
            }
            $re=$this->sys->accountModel->update($data,$id);
            if($re){
                ajaxReturn ('back','修改成功', 1 );exit;
            }else{
                ajaxReturn ('','修改失败', 0 );exit;
            }
        }
    }
    
    //PMS菜单管理
    public function region(){
        $this->leftpos=4;
        $this->loadModel('sys','menu');
		$this->loadHelper('extend');
        $sql="select * from sys_menu order by orderby asc";
        $re=$this->sys->menuModel->fetchAll($sql);
       	$result=toTree($re,'menuid','parentid','childs');
       	//echo 'aa<pre>';print_r($result);die();
        //拖动排序
        if($_POST){
            $orderby=$_POST;
            foreach($orderby['orderby'] as $k=>$v){
                $data['orderby']=$k;
                $re=$this->ecs->regionModel->update($data,"region_id='".$v."'");
            }
            ajaxReturn ('back','排序成功！', 1 );exit;
        }
        include $this->loadWidget('masterTheme');
    }
    //显示添加分类页面
	public function regionAdd(){
		$this->loadHelper('arrayHelper');
		$this->loadHelper('extend');
        
		$id = $_GET['id'];
		$action	='';
        
		$data['action']='';
		$data['id']=$id;
		$html= $this->loadAjaxView('system/regionAdd',$data);
		ajaxReturn ( $html, '', 1 );
	}
	//添加分类
	public function regionInsert(){
		$this->loadHelper('extend');
        $this->loadModel('sys','menu');
        //$arr = explode('_', $_POST['id']);
        $data = $_POST;
        $data['pin'] = "UUID()";
        if(is_numeric($_POST['id'])){
        	$data['parentid'] = $_POST['id'];
        	unset($data['id']);
        }else{
        	$data['parentid'] = 0;
        	unset($data['id']);
        }
        
		$re=$this->sys->menuModel->insert($data);
		if($re){
            ajaxReturn ( 'back', '添加成功', 1 );
		}else{
            ajaxReturn ( '', '添加失败', 0 );
		}
	}
	//编辑分类页面
	public function regionEdit(){
		$this->loadHelper('arrayHelper');
		$this->loadHelper('extend');
        $this->loadModel('sys','menu');
        
		$id = $_GET['id'];
		$sql="select * from sys_menu where menuid=".$id;
		$info =$this->sys->menuModel->fetchRow($sql);
		//echo 'aa<pre>';print_r($info);die();
		$action	=	'edit';
		$data['info']=$info;
		$data['action']=$action;
		$data['id']=$id;
		$html= $this->loadAjaxView('system/regionAdd',$data);
		ajaxReturn ( $html, '', 1 );
	}
	//编辑分类
	public function regionUpdate(){
		$this->loadHelper('extend');
        $this->loadModel('sys','menu');
        
        $id=isset($_POST['id'])?$_POST['id']:'';
        $data = $_POST;
        unset($data['id']);
        //echo "<pre>";print_r($data);exit;
        $re=$this->sys->menuModel->update($data,"menuid=$id");
		if($re){
            ajaxReturn ( 'back', '修改成功', 1 );
		}else{
            ajaxReturn ( '', '修改失败', 0 );
		}
	}
	//删除分类
	public function regionDel(){
	    $this->loadHelper('extend');
        $this->loadModel('sys','menu');
		$id=isset($_GET['id'])?$_GET['id']:0;
        
		if($id!='root'){
			$re=$this->sys->menuModel->delete("menuid='".$id."'");
			if($re){
                ajaxReturn ( 'back', '删除成功', 1 );
			}else{
                ajaxReturn ( '', '删除失败', 0 );
			}
		}else{
            ajaxReturn ( '', '该节点不能删除', 0 );
		}
	}
    
    /**
     * 系统消息管理
     */
    function messages(){
        $this->leftpos=3;
        $this->loadModel('sys','messages');
        $re=$this->sys->messagesModel->fetchAll("select * from sys_messages where 1=1 order by created desc");
        
        include $this->loadWidget('masterTheme');
    }
    /**
     * 添加修改 系统消息
     */
    function messagesEdit(){
        $this->leftpos=3;
	    $this->loadHelper('extend');
        $this->loadModel('sys','messages');
        $id=isset($_GET['id'])?(int)($_GET['id']):0;
        $resource=$this->sys->messagesModel->fetchRow("select * from sys_messages where id=$id");
        if($_POST){
            $data=isset($_POST['data'])?$_POST['data']:'';
            $id=isset($_POST['id'])?(int)($_POST['id']):0;
            if(empty($data['content'])){
                ajaxReturn ( '', '请填写内容', 0 );
            }
            $data['created']=time();
            if($id>0){
                $re=$this->sys->messagesModel->update($data,$id);
            }else{
                $re=$this->sys->messagesModel->insert($data);
            }
            if($re){
                ajaxReturn ( 'back', '操作成功', 1 );
            }else{
                ajaxReturn ( '', '操作失败', 0 );
            }
        }
        
        include $this->loadWidget('masterTheme');
    }
    /**
     * 删除系统消息
     */
    function messagesDel(){
        $this->leftpos=3;
	    $this->loadHelper('extend');
        $this->loadModel('sys','messages');
        $id=isset($_GET['id'])?(int)($_GET['id']):0;
        $re=$this->sys->messagesModel->delete($id);
        if($re){
            ajaxReturn ( 'back', '操作成功', 1 );
        }else{
            ajaxReturn ( '', '操作失败', 0 );
        }
    }
    
    /**
     * 加盟申请
     */
    function join(){
        $this->leftpos=2;
        $this->loadModel('sys','join');
        $re=$this->sys->joinModel->fetchAll("select * from sys_join where id>0 order by status,created desc");
        
        include $this->loadWidget('masterTheme');
    }
    /**
     * 加盟状态修改
     */
    function joinStatus(){
        $this->leftpos=2;
        $this->loadModel('sys','join');
	    $this->loadHelper('extend');
        $id=isset($_GET['id'])?(int)($_GET['id']):0;
        $data['status']=isset($_GET['sta'])?(int)($_GET['sta']):2;
        $re=$this->sys->joinModel->update($data,$id);
        if($re){
            ajaxReturn ( 'back', '操作成功', 1 );
        }else{
            ajaxReturn ( '', '操作失败', 0 );
        }
    }
    
    //结算账户列表
    function financial(){
        $this->leftpos=5;
        $this->loadModel('pms','bank');
        $this->loadModel('pms','account');
        
        $re=$this->pms->bankModel->fetchAll("select a.*,b.userName,b.hotelName from pms_bank as a left join pms_account as b on b.id=a.pmsId");
        
        include $this->loadWidget('masterTheme');
    }
    //结算账户审核操作
    function financialEdit(){
        $this->loadModel('pms','bank');
	    $this->loadHelper('extend');
        $id=isset($_GET['id'])?(int)($_GET['id']):0;
        $data['status']=isset($_GET['status'])?(int)($_GET['status']):0;
        $re=$this->pms->bankModel->update($data,"id=$id");
        if($re){
            ajaxReturn ( 'back', '操作成功', 1 );
        }
        ajaxReturn ( '', '操作失败', 0 );
    }
	
	//渠道列表
	function channels(){
        $this->leftpos=6;
        $this->loadHelper('pager');
		$this->loadModel('pms','channels');
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
		$size = 10;
        $offset = ($page - 1) * $size;
		$reto=$this->pms->channelsModel->fetchAll("select * from pms_channels");
		
		$count=count($reto);
        $number = ceil( $count / $size );
        $extend = new pager();
        $pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
        
        $re=array_slice($reto,$offset,$size);
		
		include $this->loadWidget('masterTheme');
	}
	//更新渠道
	function upChannels(){
		$this->loadApi('index');
        $osiiindex = new index();
		$reosii = $osiiindex->GetOsii_channels();
		if($reosii){
			exit(json_encode(array('state'=>1,'html'=>'更新成功！')));
		}
		exit(json_encode(array('state'=>0,'html'=>'无更新数据！')));
	}
}
?>